﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace System.Net.Sockets
{
    public static class SocketExtensions
    {



        //设置心跳
        public static void SetPing(this Socket tmpsock)
        {
            int keepAlive = -1744830460; // SIO_KEEPALIVE_VALS
            byte[] inValue = new byte[] { 1, 0, 0, 0, 0x20, 0x4e, 0, 0, 0xd0, 0x07, 0, 0 }; //True, 20 秒, 2 秒
            tmpsock.IOControl(keepAlive, inValue, null);
        }



        public static void SetKeepAlive(this Socket sock)
        {
            uint dummy = 0;
            byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
            BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
            BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy));
            BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);
            sock.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
        }

        public static void SetDD(this Socket sock)
        {
            byte[] inOptionValues = new byte[4 * 3];
            BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0);
            BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, 4);
            BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, 8);
            sock.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
        }
    }
}